'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Windows.Forms

Public Class aaNet
    Public ISPReturnData As New ISPReturnData
    Private CookieContainer As New System.Net.CookieContainer

    Public Sub New(ByVal ConfigurationData As ConfigurationData)

        'set rollover date
        SetRolloverDate(ConfigurationData, ISPReturnData)

        Try
            Dim strHTMLData As String

            strHTMLData = ISPGet("https://www.aanet.com.au/aau.php?" & ConfigurationData.Username & "," & ConfigurationData.Password, CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            'upload (bytes), download (bytes), last update, next anniversary, included data (MB), Speed, Plan Name, IP, remaining quota (MB), excess usage charge ($ per GB), Current excess charge ($), Sign-up date, Name
            'strHTMLData = "5034415326,7316697876,2007-06-12 15:30:24,2007-07-03,8192,1500/256,1500/Sp,202.63.100.100,1214.25,3.00,0.00,2007-02-05,Firstname,Lastname"
            'strHTMLData = "6325861918,24655983574,2007-07-21 11:30:23,2007-08-07,49152,1500/256,1500-T3       ,203.171.74.64  ,25638.22,4.00,0   ,2007-05-08,Firstname,Lastname"
            'strHTMLData = "840836594 ,2005930844 ,2009-04-02 23:00:54,2009-04-29,0    ,ADSL2+  ,5-dollar-adsl2,255.255.255.255,-1913   ,1.50,2.80,2009-03-31,Firstname,Lastname"

            If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

            'process response
            If strHTMLData.Contains("Login Failed") = True Then
                ISPReturnData.ErrorMessage = "Login Failed"
                Exit Sub
            End If

            Dim Cost As String = ""

            Dim strlines() As String
            strlines = strHTMLData.Split(vbLf.ToCharArray)

            For Each strLine As String In strlines
                strLine = Trim(strLine)
                If strLine.StartsWith("<") = False Then
                    Dim strData() As String
                    strData = strHTMLData.Split(",".ToCharArray)
                    ISPReturnData.PeakUpload = CSng(strData(0)) / 1024 / 1024
                    ISPReturnData.PeakDownload = CSng(strData(1)) / 1024 / 1024
                    'ISPReturnData.LastUpdated = CDate(strData(2))
                    ISPReturnData.LastUpdated = Date.ParseExact(strData(2), "yyyy-MM-dd HH:mm:ss", Nothing)
                    Cost = strData(10)
                End If
            Next

            '##########################################################

            ProcessISPData(ConfigurationData, ISPReturnData)

            'check for error
            If ISPReturnData.ErrorMessage <> "" Then Exit Sub

            '##########################################################

            'create graph data

            'downloads
            ISPReturnData.GraphItemList.Add(New GraphItem("Downloads " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), False))

            'uploads
            ISPReturnData.GraphItemList.Add(New GraphItem("Uploads " & Format(System.Math.Floor((ISPReturnData.PeakUpload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakUpload & " MB)", ISPReturnData.PeakUpload, CSng(Val(ConfigurationData.PeakUsageLimit)), False))

            'days elapsed
            ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

            '##########################################################

            'create stats data
            ISPReturnData.TextItemList.Add(New TextItem("D/L Left :", ISPReturnData.PeakUsageRemainingDescription))
            ISPReturnData.TextItemList.Add(New TextItem("D/L Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))

            'upload
            ISPReturnData.TextItemList.Add(New TextItem("U/L Left :", (Val(ConfigurationData.PeakUsageLimit) - ISPReturnData.PeakUpload).ToString & " MB"))
            If ISPReturnData.DaysLeft > 0 Then
                ISPReturnData.TextItemList.Add(New TextItem("U/L Avg. Left :", (CInt(Val((Val(ConfigurationData.PeakUsageLimit) - ISPReturnData.PeakUpload) / ISPReturnData.DaysLeft)) & " MB/Day (" & ISPReturnData.DaysLeft & " Days)")))
            Else
                ISPReturnData.TextItemList.Add(New TextItem("U/L Avg. Left :", (CInt(Val((Val(ConfigurationData.PeakUsageLimit) - ISPReturnData.PeakUpload))) & " MB/Day (" & ISPReturnData.DaysLeft & " Days)")))
            End If

            ISPReturnData.TextItemList.Add(New TextItem("Excess Cost :", "$" & Cost))

            '##########################################################

            'process alerts
            ProcessAlerts(ConfigurationData, ISPReturnData)

        Catch ex As Exception
            ISPReturnData.ErrorMessage = ex.Message
        End Try
    End Sub
End Class
